package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartLegendData;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 雷达图
 *
 * @author chart
 */
@RestController
@RequestMapping("/radar/chart")
public class RadarChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;




    @PostMapping("/importData1")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }
            List<String> xdata = new ArrayList<>();
            List<String> xList = getdata.get(0);
            if (!CollectionUtils.isEmpty(xList)) {
                for (int i = 0; i < xList.size(); i++) {
                    String s = xList.get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        xdata.add(s);
                    }
                }
            }


            List<JSONObject> datas = new ArrayList<>();

            for (int i = 1; i < getdata.size(); i++) {
                List<List<Object>> groupList = new ArrayList<>();
                String groupName = new String();
                List<String> colData = getdata.get(i);
                for (int i1 = 0; i1 < colData.size(); i1++) {
                    String s = colData.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1 == 0) {
                            groupName = s;
                        } else {
                            List<Object> cellData = new ArrayList<>();
                            cellData.add(Double.parseDouble(s));
                            groupList.add(cellData);
                        }
                    }
                }

                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("value", groupList);
                jsonObject2.put("name", groupName);
                jsonObject2.put("areaStyle", new ArrayList<>());
                datas.add(jsonObject2);
            }


            List<ChartLegendData> radarList = new ArrayList<>();
            for (int i = 1; i < xdata.size(); i++) {
                ChartLegendData chartLegendData = new ChartLegendData();
                chartLegendData.setName(xdata.get(i));
                radarList.add(chartLegendData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            List<JSONObject> radar = (List<JSONObject>) jsonObject.get("radar");
            for (int i = 0; i < radar.size(); i++) {
                JSONObject jsonObject1 = radar.get(i);
                jsonObject1.put("indicator", radarList);

                JSONObject jsonObject2 = series.get(i);
                jsonObject2.put("data", datas);
            }
            jsonObject.put("radar", radar);

            jsonObject.put("series", series);

            ArrayList<Map> maps = new ArrayList<>();
            JSONObject legend = (JSONObject) jsonObject.get("legend");
            for (int i = 0; i < datas.size(); i++) {
                JSONObject jsonObject1 = datas.get(i);
                String name = (String) jsonObject1.get("name");
                HashMap<String, String> map = new HashMap<>();
                map.put("name",name);
                maps.add(map);
            }
            legend.put("data",maps);
            jsonObject.put("legend",legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }
    private List<List<String>> wrapListMapToList(List<Map<String, Object>> dataList) {
        List<List<String>> data = new ArrayList<>();//  [["1","4"],[33,666]]
        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                rowData.add(entry.getValue().toString());
            }
            data.add(rowData);
        }
        return data;
    }


    @PostMapping("/v2/importData1")
    public AjaxResult vimportData(@RequestBody List<Map<String, Object>> dataList,Long configId) throws Exception {



        try {

/*            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = convertDataListToSheetRemoveNineAndOne(dataList);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }
            List<String> xdata = new ArrayList<>();
            List<String> xList = getdata.get(0);
            if (!CollectionUtils.isEmpty(xList)) {
                for (int i = 0; i < xList.size(); i++) {
                    String s = xList.get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        xdata.add(s);
                    }
                }
            }


            List<JSONObject> datas = new ArrayList<>();

            for (int i = 1; i < getdata.size(); i++) {
                List<List<Object>> groupList = new ArrayList<>();
                String groupName = new String();
                List<String> colData = getdata.get(i);
                for (int i1 = 0; i1 < colData.size(); i1++) {
                    String s = colData.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1 == 0) {
                            groupName = s;
                        } else {
                            List<Object> cellData = new ArrayList<>();
                            cellData.add(Double.parseDouble(s));
                            groupList.add(cellData);
                        }
                    }
                }

                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("value", groupList);
                jsonObject2.put("name", groupName);
                jsonObject2.put("areaStyle", new ArrayList<>());
                datas.add(jsonObject2);
            }


            List<ChartLegendData> radarList = new ArrayList<>();
            for (int i = 1; i < xdata.size(); i++) {
                ChartLegendData chartLegendData = new ChartLegendData();
                chartLegendData.setName(xdata.get(i));
                radarList.add(chartLegendData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            List<JSONObject> radar = (List<JSONObject>) jsonObject.get("radar");
            for (int i = 0; i < radar.size(); i++) {
                JSONObject jsonObject1 = radar.get(i);
                jsonObject1.put("indicator", radarList);

                JSONObject jsonObject2 = series.get(i);
                jsonObject2.put("data", datas);
            }
            jsonObject.put("radar", radar);

            jsonObject.put("series", series);

            ArrayList<Map> maps = new ArrayList<>();
            JSONObject legend = (JSONObject) jsonObject.get("legend");
            for (int i = 0; i < datas.size(); i++) {
                JSONObject jsonObject1 = datas.get(i);
                String name = (String) jsonObject1.get("name");
                HashMap<String, String> map = new HashMap<>();
                map.put("name",name);
                maps.add(map);
            }
            legend.put("data",maps);
            jsonObject.put("legend",legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {

        }
    }

    /**
     *移除第九行第一列
     * @param dataList
     * @return
     */
    public static Sheet convertDataListToSheetRemoveNineAndOne(List<Map<String, Object>> dataList) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        for (int i = 0; i < 8; i++) {
            sheet.createRow(i);
        }


        int rowIndex = 8;
        for (Map<String, Object> data : dataList) {
            Row row = sheet.createRow(rowIndex++);
            int cellIndex = 0;
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                Cell cell = row.createCell(cellIndex++);
                setCellValue(cell, entry.getValue());
            }
        }

        if (sheet.getRow(8) != null && sheet.getRow(8).getCell(0) != null) {
            sheet.getRow(8).removeCell(sheet.getRow(8).getCell(0));
        }

        return sheet;
    }

    private static void setCellValue(Cell cell, Object value) {
        if (value instanceof String) {
            cell.setCellValue((String) value);
        } else if (value instanceof Number) {
            cell.setCellValue(((Number) value).doubleValue());
        } else if (value instanceof Boolean) {
            cell.setCellValue((Boolean) value);
        } else if (value instanceof java.util.Date) {
            cell.setCellValue((java.util.Date) value);
        } else {
            cell.setCellValue(value != null ? value.toString() : "");
        }
    }


    /**
     * 不移除第九行第一列
     * @param dataList
     * @return
     */
    public static Sheet convertDataListToSheet(List<Map<String, Object>> dataList) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        // Create the first 8 rows
        for (int i = 0; i < 8; i++) {
            sheet.createRow(i);
        }

        // Start populating from the ninth row
        int rowIndex = 8;
        for (Map<String, Object> data : dataList) {
            Row row = sheet.createRow(rowIndex++);
            int cellIndex = 0;
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                Cell cell = row.createCell(cellIndex++);
                setCellValue(cell, entry.getValue());
            }
        }

        return sheet;

}}
